package main
import (
	"fmt"
	"net"
	"time"
	"strings"
)

func main(){
	
	defer time.Sleep(time.Second * 3)	//延时3秒
	
	/*第一步：监听端口*/
	listen, err := net.Listen("tcp", "127.0.0.1:8000")
	if err != nil{
		fmt.Println("Listen err ：", err)
		return
	}
	fmt.Println("Listen 127.0.0.1:8000 OK")
	defer listen.Close()


	/*第二步，循环接收客户端信号*/
	for{
		conn, err := listen.Accept()
		if err != nil{
			fmt.Println("Accept err ：", err)
			return
		}
		
		/*第三步，响应客户端信息*/
		go Xiangying(conn)
	}
}

func Xiangying(conn net.Conn){
	defer conn.Close()
	addr := conn.RemoteAddr()
	fmt.Println(addr, "已经连接上线")
	conn.Write([]byte("你已经连接成功"))
	
	/*第四步，读取客户端信息*/
	buf := make([]byte, 4096)
	for{
		sum, err := conn.Read(buf)
		if err != nil{
			fmt.Println("Read err : ", err)
			return
		}
		
		/*第五步，输出客户端消息*/
		fmt.Println(addr, " ：", string(buf[:sum]))
		str := strings.ToUpper(string(buf[:sum]))
		conn.Write([]byte(str))
	}
	
	
}